package linkedList;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
       /* HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(heroNode1);
        doubleLinkedList.add(heroNode3);
        doubleLinkedList.add(heroNode2);
        doubleLinkedList.add(heroNode4);
        doubleLinkedList.list();
        System.out.println("修改");
        doubleLinkedList.update(new HeroNode2(4, "林冲~~~", "豹子头~~~"));
        doubleLinkedList.list();
        System.out.println("删除");
        doubleLinkedList.delete(4);
        doubleLinkedList.delete(3);
        doubleLinkedList.list();*/
        HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.addByNo(heroNode1);
        doubleLinkedList.addByNo(heroNode3);
        doubleLinkedList.addByNo(heroNode2);
        doubleLinkedList.addByNo(heroNode4);
        doubleLinkedList.list();
    }
}

class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(0, "", "");

    //获得头节点
    public HeroNode2 getHead() {
        return head;
    }

    public void setHead(HeroNode2 head) {
        this.head = head;
    }

    //添加节点到双向链表
    public void add(HeroNode2 heroNode) {
        //创建临时头节点
        HeroNode2 temp = head;
        while(true){
            if(temp.next!= null){
                temp = temp.next;
            }else {
                break;
            }
        }
        //将节点插入到双向链表中
        temp.next = heroNode;
        heroNode.pre = temp;
    }
    public void addByNo(HeroNode2 heroNode) {

        HeroNode2 temp = head;
        while(true){
            if(temp.next== null){
                //该节点是尾节点
                temp.next = heroNode;
                heroNode.pre = temp;
                return;
            }
            if(temp.no == heroNode.no){
                //节点已存在
                System.out.println("节点已存在");
                return;
            }
            if(temp.next.no > heroNode.no){
                //找到插入位置
                heroNode.next = temp.next;
                temp.next.pre = heroNode;
                temp.next = heroNode;
                heroNode.pre = temp;
                return;
            }
            temp = temp.next;
        }

    }


    //遍历链表
    public void list() {
        //先判断这个链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //创建临时变量存储
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public void update(HeroNode2 newHeroNode){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while(true){
            if(temp.no == newHeroNode.no){
                temp.name = newHeroNode.name;
                temp.nickname = newHeroNode.nickname;
                System.out.println("修改成功");
                break;
            }
            if(temp.next == null){
                System.out.println("没有找到该节点");
                break;
            }
            temp = temp.next;
        }
    }
    public void delete(int no){
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while(true){
            if(temp.no == no){
                //找到了该节点
                temp.pre.next = temp.next;
                if(temp.next != null){
                    temp.next.pre = temp.pre;
                }
                System.out.println("删除成功");
                break;
            }
            if(temp.next == null){
                System.out.println("没有找到该节点");
                break;
            }
            temp = temp.next;
        }
    }
}

//节点
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}